package com.chance.cc.crawler.development.bootstrap.xiaohongshu;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.utils.RedisReader;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPageItem;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Field_Keword;
import static org.codehaus.groovy.runtime.EncodingGroovyMethods.md5;

public class XHSArticleImport {

    private static final String domainId = "xhs";
    public static final String site= "xhs-article";
    public static final String site_biz= "search";
    public static final String wxappArticleUrlFormat = "https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/note/%s/single_feed";


    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("H7307T4706B25G4D");
        proxy.setPassword("05B4877CC39192C0");
    }

    public static void main(String[] args) throws ParseException, NoSuchAlgorithmException, IOException {
        articleImport();
    }
    private static void articleImport() throws IOException, NoSuchAlgorithmException, ParseException {
        List<String> readLines = IOUtils.readLines(new FileInputStream("D:\\chance\\soft\\xhs\\xhs20190329.txt"), "utf-8");
        for (String readLine : readLines) {
            if (readLine.startsWith("KOL")){
                continue;
            }
            String[] split = readLine.split("\t");
            String releaseTime = split[4];
            String noteUrl = split[6];
            String[] urls = noteUrl.split("/");
            String noteId = urls[urls.length -1];
            String keyword = split[8];
            keyword = keyword.replaceAll("\"","");

            String articleUrl = String.format(wxappArticleUrlFormat, noteId);
            String urlPath = articleUrl.substring(articleUrl.indexOf("/fe_api/"));
            String x_sign = "X" + md5(urlPath + "WSUDD");

            CrawlerRequestRecord crawlerArticleRequestRecord = CrawlerRequestRecord.builder()
                    .startPageRequest("xhs",turnPageItem)
                    .httpUrl(articleUrl)
                    .httpHead("x-sign",x_sign)
                    .releaseTime(dateToTimestamp(releaseTime))
                    .needWashed(true)
                    .proxy(proxy)
                    .build();
            crawlerArticleRequestRecord.tagsCreator().bizTags().addCustomKV(Tag_Field_Keword,keyword);

            crawlerArticleRequestRecord.tagsCreator().bizTags().addCustomKV("noteId",noteId);
            crawlerArticleRequestRecord.tagsCreator().bizTags().addCustomKV("releaseTime",releaseTime);
            crawlerArticleRequestRecord.tagsCreator().bizTags().addSite(site);
            crawlerArticleRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
            crawlerArticleRequestRecord.tagsCreator().bizTags().addDomain(domainId);

            String requestKey = crawlerArticleRequestRecord.getRecordKey();
            //确保key值唯一
            String shaKey = DigestUtils.sha1Hex(System.currentTimeMillis()+requestKey);
            //push kv first
            RedisReader redisReader = new RedisReader("192.168.1.214",6379,3);

            redisReader.mapPush("crawler-xhs-xhs-article-queue_map",shaKey, JSON.toJSONString(crawlerArticleRequestRecord));
            redisReader.listPush("crawler-xhs-xhs-article-queue_list",shaKey);
        }
    }

    public static long dateToTimestamp(String dataStr) throws ParseException {
        String regEx="[^0-9]+"; //正则表达式，用于匹配非数字串，+号用于匹配出多个非数字串
        Pattern pattern = Pattern.compile(regEx);

        if (dataStr.equals("刚刚")){
            return System.currentTimeMillis();
        } else if (Pattern.matches("\\d*秒前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*1000L);
        } else if (Pattern.matches("\\d*分钟前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*60*1000L);
        } else if (Pattern.matches("\\d*小时前",dataStr)){
            int number = Integer.valueOf(pattern.split(dataStr)[0]);
            return (System.currentTimeMillis() - number*60*60*1000L);
        } else if (Pattern.matches("\\d*小时\\d*分钟前",dataStr)){
            int hourNumber = Integer.valueOf(pattern.split(dataStr)[0]);
            int secondNumber = Integer.valueOf(pattern.split(dataStr)[1]);
            long totalMillis = hourNumber*60*60*1000L + secondNumber*60*1000L;
            return (System.currentTimeMillis() - totalMillis);
        } else if (dataStr.startsWith("今天")){
            String currentTime = DateFormatUtils.format(System.currentTimeMillis(), TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("今天", currentTime);
        } else if (dataStr.startsWith("昨天")){
            String yesterdayTime = DateFormatUtils.format(System.currentTimeMillis() - 60*60*24*1000L, TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("昨天", yesterdayTime);
        } else if (dataStr.startsWith("前天")){
            String beforeYesterdayTime = DateFormatUtils.format(System.currentTimeMillis() - 2*60*60*24*1000L, TimeForamtEnum.format1.getFormat());
            dataStr = dataStr.replace("前天", beforeYesterdayTime);
        } else if (Pattern.matches("\\d{2}月\\d{2}[日]*", dataStr)){
            Calendar cal = Calendar.getInstance();
            dataStr = cal.get(Calendar.YEAR)+"年"+dataStr;
        } else if (Pattern.matches("\\d{2}-\\d{2}", dataStr)){
            Calendar cal = Calendar.getInstance();
            dataStr = cal.get(Calendar.YEAR)+"-"+dataStr;
        }
        return DateUtils.parseDateStrictly(dataStr, TimeForamtEnum.allFormats()).getTime();
    }

    public enum TimeForamtEnum {
        format1("yyyy年MM月dd日"),
        format1_1("yyyy年MM月dd日 HH:mm:ss"),
        format1_2("yyyy年MM月dd日 HH:mm"),
        format1_3("yyyy年MM月dd日HH:mm"),
        format1_4("yyyy年MM月dd日 HH点mm分"),
        format1_5("yyyy年MM月dd日 HH点mm分ss秒"),
        format1_6("yyyy年MM月dd日HH点mm分"),
        format1_7("yyyy年MM月dd日HH点mm分ss秒"),


        format4("yyyy-MM-dd HH:mm:ss"),
        format5("yyyy-MM-dd HH:mm"),
        format6("yyyy-MM-dd"),

        format7("yyyy/MM/dd HH:mm:ss"),
        format7_1("MM/dd/yyyy HH:mm:ss"),
        format8("yyyy/MM/dd HH:mm"),
        format9("yyyy/MM/dd"),

        format10("yyyy.MM.dd HH:mm:ss"),
        format11("yyyy.MM.dd HH:mm"),
        format12("yyyy.MM.dd"),

        format13("EEE MMM d HH:mm:ss +0800 yyyy");

        private String format;

        private TimeForamtEnum(String format) {
            this.format = format;
        }

        public static String[] allFormats() {
            TimeForamtEnum[] timeForamtEnums = TimeForamtEnum.values();
            String[] formats = new String[timeForamtEnums.length];
            for (int count = 0; count < timeForamtEnums.length; count++) {
                formats[count] = timeForamtEnums[count].format;
            }
            return formats;
        }

        public String getFormat() {
            return format;
        }
    }
}
